//
//  MEFlickrUser.m
//  FlickrImport
//
//  Created by Sandeep Chayapathi on 1/18/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "MEFlickrUser.h"
#import "MEFlickrConnection.h"

@implementation MEFlickrUser

- (id) init {
    self = [super init];
    [self setNsId:nil];
    [self setUserName:nil];
    [self setRealName:nil];

    return self;
}

- (id) initWithDictionary: (NSDictionary *) dict {
    self = [super init];
    userDict = [dict retain];
    
    [self setNsId: [dict valueForKey:@"_nsid"]];
    [self setUserName: [dict valueForKeyPath:@"username.$"]];
    [self setRealName: [dict valueForKeyPath:@"realname.$"]];
    [self setIconServer:[dict valueForKey:@"_iconserver"]];
    [self setIconFarm: [dict valueForKey:@"_iconfarm"]];
    [self setPhotoCount:[[dict valueForKeyPath:@"photos.count.$"] intValue]];
    [self setLocation: [dict valueForKeyPath: @"location.$"] ];
    
    return self;
}


- (void) dealloc {
    [nsId release];
    [userName release];
    [realName release];
    
    [super dealloc];
}

// Getters and Setters
- (NSString *) location; {
    return location;
}

- (void) setLocation: (NSString *) tmp; {
    tmp = [tmp copy];
    [location release];
    location = tmp;
}

- (int) photoCount; {
    return photoCount;
}

- (void) setPhotoCount: (int) tmp; {
    photoCount = tmp;
}


- (NSString *) userName;{
    return userName;
}

- (void) setUserName: (NSString *) tmp;{
    tmp = [tmp copy];
    [userName release];
    userName = tmp;
}

- (NSString *) realName; {
    return realName;
}

- (void) setRealName: (NSString *) tmp; {
    tmp = [tmp copy];
    [realName release];
    realName = tmp;
}

- (NSString *) nsId; {
    return nsId;
}

- (void) setNsId: (NSString *) tmp; {
    tmp = [tmp copy];
    [nsId release];
    nsId = tmp;
}

- (NSString *) iconServer; {
    return iconServer;
}

- (void) setIconServer: (NSString *) tmp; {
    iconServer = tmp;
}

- (NSString *) iconFarm; {
    return iconFarm;
}

- (void) setIconFarm: (NSString *) tmp; {
    iconFarm = tmp;
}

- (int) friend; {
    return friend;
}

- (void) setFriend: (int) tmp; {
    friend = tmp;
}

- (int) family; {
    return family;
}

- (void) setFamily: (int) tmp; {
    family = tmp;
}

- (int) ignored; {
    return ignored;
}

- (void) setIgnored: (int) tmp; {
    ignored = tmp;
}

// When an instance is assigned as objectValue to a NSCell, the NSCell creates a copy.
// Therefore we have to implement the NSCopying protocol
- (id)copyWithZone:(NSZone *)zone {
    MEFlickrUser *copy = [[[self class] allocWithZone: zone] initWithDictionary: userDict];
    return copy;
}

- (NSImage *) buddyIcon {
    MEFlickrConnection *conn = [MEFlickrConnection getConnectionWithDelegate:self];
    OFFlickrContext *context = [conn context];
    
    NSURL *tmp_url = [NSURL URLWithString: [context buddyIconURLWithUserID:[self nsId]
                                                                iconServer:[self iconServer]
                                                                  iconFarm:[self iconFarm]] ];
    
    NSImage *icon = [[NSImage alloc] initWithContentsOfURL: tmp_url];
    
    return icon;
}

- (NSString *) primaryDescription; {
    return ([self realName]) ? [self realName]:[self userName];    
}

- (NSString *) secondaryDescription; {
    NSString *tmp = [NSString stringWithFormat:@"%@  %i photos",
           ([self location]) ? [self location]:@" ",
        [self photoCount]];
    
    return tmp;
    
}

@end
